<template>
  <div>
    <el-form ref="deptForm" :rules="rules" :model="form" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <el-option v-for="(item, index) in userList" :key="index" :value="item.username" :label="item.username" />
          <!-- <el-option value="2" label="大漂亮" /> -->
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
        <el-button size="small" @click="hCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { AddDepartmentsList, ChangeDepartmentsList, GetSysUserSimple, OneDepartmentsList } from '@/api/departments'
export default {
  props: {
    myId: {
      type: String,
      required: true
    },
    formChange: {
      type: Boolean,
      required: true
    },
    codeList: {
      type: Array,
      required: true
    }
  },
  data() {
    const validCode = (rule, value, callback) => {
      if (!this.codeList.includes(value)) {
        callback(`部门编码 ${value} 重复`)
      } else {
        callback()
      }
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      userList: [], // 下拉框角色列表
      rules: {
        name: [
          // { validator: validCode, trigger: 'blur' },
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' }
        ],
        code: [
          { validator: validCode, trigger: 'blur' },
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getUserList()
    if (this.formChange) this.getOne()
  },
  methods: {
    // 获取一个要修改的表单
    async getOne() {
      const res = await OneDepartmentsList(this.myId)
      this.form = res.data
      // console.log(res)
    },
    // 获取select内容
    async getUserList() {
      // console.log(this.myId)
      const res = await GetSysUserSimple()
      this.userList = res.data
    },
    // 添加部门
    hAdd() {
      try {
        this.form.pid = this.myId
        AddDepartmentsList(this.form)
        this.$emit('hSuccess')
      } catch (error) {
        (err) => err
      }
    },

    // 确定
    hSubmit() {
      this.$refs.deptForm.validate((valid) => {
        if (!valid) return
        this.formChange ? this.hCancel() : this.hAdd()
      })
    },
    // 取消
    hCancel() {
      ChangeDepartmentsList(this.myId, this.form)
      this.$emit('hSuccess')
    }
  }
}
</script>
